#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<unordered_map>

using namespace std;


string minWindow(string s, string t) {
    unordered_map<char, int> m1;
    unordered_map<char, int> m2;
    for (auto e : t)
    {
        m1[e]++;
    }
    int size = m1.size();

    int ct = 0, ret = INT_MAX, begin = 0;
    for (int left = 0, right = 0; right < s.size(); right++) {
        m2[s[right]]++;
        if (m2[s[right]] == m1[s[right]]) {
            ct++;
        }

        if (ct == size) {
            if (ret > right - left + 1) {
                begin = left;
                ret = right - left + 1;
            }

            while (ct == size)
            {
                if (m2[s[left]] == m1[s[left]])
                {
                    if (ret > right - left + 1) {
                        begin = left;
                        ret = right - left + 1;
                    }
                    ct--;
                }
                m2[s[left]]--;
                left++;
            }
        }
    }
    if (ret == INT_MAX)
        return "";
    else
        return string(s.begin() + begin, s.begin() + begin + ret);

}

string minWindow(string s, string t) {
    unordered_map<char, int> m1;
    unordered_map<char, int> m2;
    for (auto e : t) {
        m1[e]++;
    }
    int size = m1.size();

    int ct = 0, ret = INT_MAX, begin = 0;
    for (int left = 0, right = 0; right < s.size(); right++) {
        m2[s[right]]++;
        if (m2[s[right]] == m1[s[right]]) {
            ct++;
        }
        while (ct == size) {
            if (m2[s[left]] == m1[s[left]]) {
                if (ret > right - left + 1) {
                    begin = left;
                    ret = right - left + 1;
                }
                ct--;
            }
            m2[s[left]]--;
            left++;
        }
    }

    if (ret == INT_MAX)
        return "";
    else
        return string(s.begin() + begin, s.begin() + begin + ret);
}
int main()
{
    /*string s = "ADOBECODEBANC";
    string t = "ABC";
    cout << minWindow(s, t) << endl;*/
    string s = "caccaacaaaabbcaccaccc";
    string t = "acccacbccc";
    cout << minWindow(s, t) << endl;

	return 0;
}